package com.joy.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joy.commonutils.R;
import com.joy.eduservice.entity.EduCourse;
import com.joy.eduservice.entity.vo.CourseInfoVo;
import com.joy.eduservice.entity.vo.CoursePublishVo;
import com.joy.eduservice.entity.vo.CourseQuery;
import com.joy.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-02
 */
@Api(description = "课程管理之添加课程信息")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;


    //添加课程信息的方法
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.saveCourseInfo(courseInfoVo);

        return R.ok();
    }

    //根据课程id查询课程的基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfo);
    }

    //分页查询课程的方法
    @GetMapping("/pageListCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,@PathVariable long limit){
        Page<EduCourse> eduCoursePage = new Page<>(current, limit);
        eduCourseService.page(eduCoursePage,null);

        //查出总共有几条记录
        long total = eduCoursePage.getTotal();
        //获取装有记录的所有集合,用于显示的时候用
        List<EduCourse> records = eduCoursePage.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //多条件的分页显示
    @GetMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> eduCoursePage = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String name = courseQuery.getName();
        String status = courseQuery.getStatus();

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        eduCourseService.page(eduCoursePage,wrapper);
        long total = eduCoursePage.getTotal();
        List<EduCourse> records = eduCoursePage.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //根据课程id修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程的最终发布
    //修改课程的状态
    @GetMapping("/publishCourse/{id}")
    public R publishCourse(String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布的状态
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @GetMapping("/deleteCourse/{id}")
    public R deleteCourse(@PathVariable String id){
        eduCourseService.removeCourse(id);
        return R.ok();
    }

}

